package com.work.game.ad;

import android.app.Activity;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationAdEcpmInfo;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager;
import com.work.game.ad.inter.RewardAdFullAdInter;
import com.work.game.view.LoadingDailog;

import cn.hutool.core.util.ObjectUtil;

/**
 * 插屏广告
 */
public class MediationTablePlaqueAd {

    private String codeId;
    private RewardAdFullAdInter rewardAdFullAdInter;
    private TTFullScreenVideoAd ttFullScreenVideoAd;

    private MediationTablePlaqueAd() {
    }

    public static volatile MediationTablePlaqueAd instance;

    public static synchronized MediationTablePlaqueAd getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (MediationTablePlaqueAd.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new MediationTablePlaqueAd();
                }
            }
        }
        return instance;
    }

    public void init(String codeId) {
        this.codeId = codeId;
    }

    public void setRewardAdFullAdInter(RewardAdFullAdInter rewardAdFullAdInter) {
        this.rewardAdFullAdInter = rewardAdFullAdInter;
    }

    //加载插全屏广告
    public void loadCacheInterstitialFullAd(Activity activity) {
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity);
        adNativeLoader.loadFullScreenVideoAd(buildInterstitialFullAdSlot(), new TTAdNative.FullScreenVideoAdListener() {
            @Override
            public void onError(int errorCode, String errorMsg) {
            }

            @Override
            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ttFullScreenVideoAd) {
                //广告加载成功
            }

            @Override
            public void onFullScreenVideoCached() {
                //广告缓存成功 此api已经废弃，请使用onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd)
            }

            @Override
            public void onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd) {
                //广告缓存成功 在此回调中进行广告展示
                getInstance().ttFullScreenVideoAd = ttFullScreenVideoAd;
            }
        });
    }

    //展示插全屏广告
    public void showCacheInterstitialFullAd(Activity activity, LoadingDailog loadingDailog) {
        if (activity == null || ttFullScreenVideoAd == null) {
            loadInterstitialFullAd(activity, loadingDailog);
            return;
        }
        ttFullScreenVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {
            @Override
            public void onAdShow() {
                //广告展示
                MediationBaseManager manager = ttFullScreenVideoAd.getMediationManager();
                //获取展示广告相关信息，需要再show回调之后进行获取
                if (manager != null && manager.getShowEcpm() != null) {
                    MediationAdEcpmInfo showEcpm = manager.getShowEcpm();
                }
            }

            @Override
            public void onAdVideoBarClick() {
                //广告点击
            }

            @Override
            public void onAdClose() {
                //广告关闭
                if (ObjectUtil.isNotNull(rewardAdFullAdInter)) {
                    rewardAdFullAdInter.onAdClose();
                }
                onDestroy();
                loadCacheInterstitialFullAd(activity);
            }

            @Override
            public void onVideoComplete() {
                //广告视频播放完成
            }

            @Override
            public void onSkippedVideo() {
                //广告跳过
            }
        });
        if (ttFullScreenVideoAd.getMediationManager().isReady()) {
            if (loadingDailog != null) {
                loadingDailog.dismiss();
            }
            ttFullScreenVideoAd.showFullScreenVideoAd(activity); //展示插全屏广告
        } else {
            loadInterstitialFullAd(activity, loadingDailog);
        }
    }


    //加载插全屏广告
    private void loadInterstitialFullAd(Activity activity, LoadingDailog loadingDailog) {
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity);
        adNativeLoader.loadFullScreenVideoAd(buildInterstitialFullAdSlot(), new TTAdNative.FullScreenVideoAdListener() {
            @Override
            public void onError(int errorCode, String errorMsg) {
                if (loadingDailog != null) {
                    loadingDailog.dismiss();
                }
                //广告加载失败
                if (ObjectUtil.isNotNull(rewardAdFullAdInter)) {
                    rewardAdFullAdInter.onError();
                }
            }

            @Override
            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ttFullScreenVideoAd) {
                //广告加载成功
            }

            @Override
            public void onFullScreenVideoCached() {
                //广告缓存成功 此api已经废弃，请使用onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd)
            }

            @Override
            public void onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd) {
                //广告缓存成功 在此回调中进行广告展示
                showInterstitialFullAd(activity, ttFullScreenVideoAd, loadingDailog);
            }
        });
    }

    //展示插全屏广告
    private void showInterstitialFullAd(Activity activity, TTFullScreenVideoAd ttFullScreenVideoAd, LoadingDailog loadingDailog) {
        if (activity == null || ttFullScreenVideoAd == null) {
            return;
        }
        ttFullScreenVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {
            @Override
            public void onAdShow() {
                //广告展示
                MediationBaseManager manager = ttFullScreenVideoAd.getMediationManager();
                //获取展示广告相关信息，需要再show回调之后进行获取
                if (manager != null && manager.getShowEcpm() != null) {
                    MediationAdEcpmInfo showEcpm = manager.getShowEcpm();
                }
            }

            @Override
            public void onAdVideoBarClick() {
                //广告点击
            }

            @Override
            public void onAdClose() {
                //广告关闭
                if (ObjectUtil.isNotNull(rewardAdFullAdInter)) {
                    rewardAdFullAdInter.onAdClose();
                }
                onDestroy();
                loadCacheInterstitialFullAd(activity);
            }

            @Override
            public void onVideoComplete() {
                //广告视频播放完成
            }

            @Override
            public void onSkippedVideo() {
                //广告跳过
            }
        });
        if (loadingDailog != null) {
            loadingDailog.dismiss();
        }
        ttFullScreenVideoAd.showFullScreenVideoAd(activity); //展示插全屏广告
    }

    //构造插全屏广告的Adlsot
    private AdSlot buildInterstitialFullAdSlot() {
        return new AdSlot.Builder().setCodeId(codeId).setAdCount(3)//广告位ID
                .setOrientation(TTAdConstant.VERTICAL)  //设置方向
                .setMediationAdSlot(new MediationAdSlot.Builder().setMuted(false).build()).build();
    }

    private void onDestroy() {
        if (ttFullScreenVideoAd != null && ttFullScreenVideoAd.getMediationManager() != null) {
            ttFullScreenVideoAd.getMediationManager().destroy();
        }
        ttFullScreenVideoAd = null;
    }
}
